package com.noizycat.ver2.view.customListView;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.noizycat.ver2.dto.TweetDto;

public class MainViewListPagerAdapter extends PagerAdapter {

    private Context mContext;
    private ArrayList<CustomListViewObject> mList;
    private int touchPosition = 0;
    private Activity mActivity;

    /**
     * コンストラクタ.
     */
    public MainViewListPagerAdapter(Context context, Activity activity) {

        mContext = context;
        mActivity = activity;
        mList = new ArrayList<CustomListViewObject>(3);
    }

    /**
     * リストにアイテムを追加する.
     * 
     * @param item
     *            アイテム
     */
    public void add(CustomListViewObject item) {

        mList.add(item);
    }

    @Override
    public int getCount() {

        // TODO 自動生成されたメソッド・スタブ
        return mList.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {

        // Object 内に View が存在するか判定する
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {

        // コンテナに追加
        ListView list = mList.get(position).getmList();
        final CustomListViewObject ob = mList.get(position);

        container.addView(list);

        list.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent paramMotionEvent) {

                // TODO 自動生成されたメソッド・スタブ
                DisplayMetrics localDisplayMetrics = new DisplayMetrics();
                mActivity.getWindowManager().getDefaultDisplay().getMetrics(localDisplayMetrics);
                int deviceWidth = localDisplayMetrics.widthPixels;
                touchPosition = ((int) (100.0F * (paramMotionEvent.getX() /
                        deviceWidth)
                        / 20.0F));
                return false;
            }
        });

        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView parent, View view,
                    int position, long id) {

                Object item = parent.getItemAtPosition(position);
                if (item instanceof TweetDto) {
                    TweetDto dto = (TweetDto) item;
                    Log.e("LOG", dto.getTweetText());
                }
                String text;
                switch (touchPosition)
                {
                default:
                    Log.e("LOG", "center" + "  " + ob.viewName);
                    text = "c";
                    // TweetActionFactory.getAction(4).execute(getActivity(),
                    // item);
                    break;
                case 0:
                    // TweetActionFactory.getAction(1).execute(getActivity(),
                    // item);
                    text = "l";
                    Log.e("LOG", "left" + "  " + ob.viewName);
                    break;
                case 4:
                    // TweetActionFactory.getAction(2).execute(getActivity(),
                    // item);
                    text = "r";
                    Log.e("LOG", "right" + "  " + ob.viewName);
                    break;
                }

                Toast.makeText(mContext, text, Toast.LENGTH_SHORT).show();

                Log.e("LOG", "center");
            }
        });
        list.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {

            @Override
            public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

                Object item = arg0.getItemAtPosition(arg2);
                if (item instanceof String) {
                    Log.e("LOG", item.toString());
                }

                switch (touchPosition)
                {
                default:
                    Log.e("LOG", "L center");
                    // TweetActionFactory.getAction(4).execute(getActivity(),
                    // item);
                    break;
                case 0:
                    // TweetActionFactory.getAction(1).execute(getActivity(),
                    // item);
                    Log.e("LOG", "L left");
                    break;
                case 4:
                    // TweetActionFactory.getAction(2).execute(getActivity(),
                    // item);
                    Log.e("LOG", "L right");
                    break;
                }
                return true;
            }
        });

        return list;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {

        container.removeView((View) object);
        // コンテナから View を削除
    }

}
